--- Day 19: An Elephant Named Joseph ---

The Elves contact you over a highly secure emergency channel. Back at the North Pole, the Elves are busy misunderstanding White Elephant parties.

Each Elf brings a present. They all sit in a circle, numbered starting with position 1. Then, starting with the first Elf, they take turns stealing all the presents from the Elf to their left. An Elf with no presents is removed from the circle and does not take turns.

For example, with five Elves (numbered 1 to 5):

  1
5   2
 4 3
Elf 1 takes Elf 2's present.
Elf 2 has no presents and is skipped.
Elf 3 takes Elf 4's present.
Elf 4 has no presents and is also skipped.
Elf 5 takes Elf 1's two presents.
Neither Elf 1 nor Elf 2 have any presents, so both are skipped.
Elf 3 takes Elf 5's three presents.
So, with five Elves, the Elf that sits starting in position 3 gets all the presents.

With the number of Elves given in your puzzle input, which Elf gets all the presents?

Your puzzle input is 3005290.

In [48]:
from tqdm import tqdm

def find_elf(total=5):
    elfs = [[e+1, 1] for e in range(total)]
    p = 0
    for _ in tqdm(range(total-1)):
        if p+1 == len(elfs):
            left_elf = elfs.pop(0)
            elfs[p-1][1] += left_elf[1]
            p = 0
        else:        
            left_elf = elfs.pop(p+1)
            elfs[p][1] += left_elf[1]
            p = (p+1) % len(elfs)
        
    return elfs[0][0]

In [49]:
%%time
find_elf()


100%|██████████| 4/4 [00:00<00:00, 27235.74it/s]
CPU times: user 8 ms, sys: 0 ns, total: 8 ms
Wall time: 4.27 ms

Out[49]:
3

In [50]:
%%time
find_elf(total=3005290)


100%|██████████| 3005289/3005289 [29:40<00:00, 1687.66it/s]  
CPU times: user 29min 36s, sys: 6.36 s, total: 29min 42s
Wall time: 29min 42s

Out[50]:
1816277

In [52]:
from math import log

def find_elf_math(n):
    if n == 1 or n == 2:
        return 1
    logn = log(n,2)
    if not logn.is_integer():
        expo = int(logn) + 1
        power = int(pow(2,expo)) - 1
        sub = power - n
        res = n - sub
        return res
    else:
        return 1

In [53]:
%%time
find_elf_math(3005290)


CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 48.6 µs
Out[53]:
1816277
--- Part Two ---

Realizing the folly of their present-exchange rules, the Elves agree to instead steal presents from the Elf directly across the circle. If two Elves are across the circle, the one on the left (from the perspective of the stealer) is stolen from. The other rules remain unchanged: Elves with no presents are removed from the circle entirely, and the other elves move in slightly to keep the circle evenly spaced.

For example, with five Elves (again numbered 1 to 5):

The Elves sit in a circle; Elf 1 goes first:
  1
5   2
 4 3
Elves 3 and 4 are across the circle; Elf 3's present is stolen, being the one to the left. Elf 3 leaves the circle, and the rest of the Elves move in:
  1           1
5   2  -->  5   2
 4 -          4
Elf 2 steals from the Elf directly across the circle, Elf 5:
  1         1 
-   2  -->     2
  4         4 
Next is Elf 4 who, choosing between Elves 1 and 2, steals from Elf 1:
 -          2  
    2  -->
 4          4
Finally, Elf 2 steals from Elf 4:
 2
    -->  2  
 -
So, with five Elves, the Elf that sits starting in position 2 gets all the presents.

With the number of Elves given in your puzzle input, which Elf now gets all the presents?

In [96]:
def elf_circle(n):
    elfs = list(range(1, n+1))    
    p = 0    
    l = len(elfs)
    for _ in tqdm(range(n-1)):
        lelf = p + (l // 2) 
        elfs.pop(lelf % l)        
        if lelf < l:
            p += 1
        l -= 1    
        p %= l        
        
    return elfs[0]

In [97]:
%%time
elf_circle(5)


100%|██████████| 4/4 [00:00<00:00, 11898.73it/s]
CPU times: user 4 ms, sys: 4 ms, total: 8 ms
Wall time: 7.93 ms

Out[97]:
2

In [98]:
%%time
elf_circle(3005290)


100%|██████████| 3005289/3005289 [20:05<00:00, 2492.46it/s] 
CPU times: user 20min 2s, sys: 4.21 s, total: 20min 6s
Wall time: 20min 5s

Out[98]:
1410967

In [ ]: